<?php
namespace app\home\controller;
use app\user\api\UserApi;

class App extends Home{
    //token其实表示的是客户发送信息的总次数
    public function sync($username = '', $password = '', $token = 0){    
        $user = new UserApi;
        $uid = $user->login($username, $password);
        if(0 < $uid){ //UC登录成功
            /* 登录用户 */
            $Member = model('Member');
            if($Member->login($uid)){ //登录用户
                if($token<0){
                    $token=0;
                }
                $me = \think\Db::name('member')->find($uid);
                if($me['send_amount']<$token){
                    //更新已消费次数
                    $me['send_amount'] = $token;
                    \think\Db::name('member')->update($me);
                }
                $data['code'] = 200;
                $data['msg'] = '登录成功';
                $data['timestamp'] = time();
                $data['d1'] = $data['timestamp'] + $me['send_max_amount'];
                $data['d2'] = $data['timestamp'] + $me['send_amount'];
                $data['d3'] = $data['timestamp'] + $me['user_max_amount'];
                $data['token'] = md5($data['timestamp'].$password);    
                return json($data);

            } else {
                return json(['code'=>500, 'msg'=>'登录失败！']);
            }
        } else { //登录失败
            switch($uid) {
                case -1: $error = '用户不存在或被禁用！'; break; //系统级别禁用
                case -2: $error = '密码错误！'; break;
                default: $error = '未知错误！'; break; // 0-接口参数错误（调试阶段使用）
            }
            return json(['code'=>500, 'msg'=>$error]);
        } 
    }
    //token其实表示此次客户消费次数
    public function msg_send_log($username = '', $password = '', $token = 0, $strlog=''){    
        $user = new UserApi;
        $uid = $user->login($username, $password);
        if(0 < $uid){ //UC登录成功
            /* 登录用户 */
            $Member = model('Member');
            if($Member->login($uid)){ //登录用户
                if($token<0){
                    $token=0;
                }
                $me = \think\Db::name('member')->find($uid);
                //更新已消费次数
                $me['send_amount'] += $token;
                \think\Db::name('member')->update($me);
                //记录此次消费
                $log['uid'] = $me['uid'];
                $log['nickname'] = $me['nickname'];
                $log['send_count'] = $token;
                $log['remark'] = $strlog;
                $log['time'] = time();
                \think\Db::name('f_wechat_log')->insert($log);

                $data['code'] = 200;
                $data['msg'] = '记录成功';
                return json($data);

            } else {
                return json(['code'=>500, 'msg'=>'登录失败！']);
            }
        } else { //登录失败
            switch($uid) {
                case -1: $error = '用户不存在或被禁用！'; break; //系统级别禁用
                case -2: $error = '密码错误！'; break;
                default: $error = '未知错误！'; break; // 0-接口参数错误（调试阶段使用）
            }
            return json(['code'=>500, 'msg'=>$error]);
        } 
    }
}